<?php

namespace App\Services;

use GuzzleHttp\Client;
use Illuminate\Support\Facades\Log;

class HeliusService
{
    protected $client;
    protected $apiKey;
    protected $baseUrl;

    public function __construct()
    {
        $this->client = new Client();
        $this->baseUrl = config('helius.rpc.url');
        $this->apiKey = config('helius.rpc.api_key');
    }

    /**
     * 获取集合所有NFT
     */
    public function getCollectionNFTs(string $collectionAddress, int $page = 1, int $limit = 1000)
    {
        try {
            $response = $this->client->post("{$this->baseUrl}/?api-key={$this->apiKey}", [
                'json' => [
                    'jsonrpc' => '2.0',
                    'id' => 1,
                    'method' => 'getAssetsByGroup',
                    'params' => [
                        'groupKey' => 'collection',
                        'groupValue' => $collectionAddress,
                        'page' => $page,
                        'limit' => $limit,
                        'sortBy' => [
                            "sortBy" => "created",
                            "sortDirection" => "asc"
                        ],
                    ]
                ]
            ]);
            return json_decode($response->getBody()->getContents(), true)['result']['items'] ?? [];
        } catch (\Exception $e) {
            Log::error("Failed to get collection NFTs: " . $e->getMessage());
            return [];
        }
    }

    /**
     * 获取NFT交易签名
     */
    public function getSignaturesForAsset(string $assetId, int $page = 1, int $limit = 100)
    {
        try {
            $response = $this->client->post("{$this->baseUrl}/?api-key={$this->apiKey}", [
                'json' => [
                    'jsonrpc' => '2.0',
                    'id' => 1,
                    'method' => 'getSignaturesForAddress',
                    'params' => [
                        $assetId,
                        ['limit' => $limit]
                    ]
                ]
            ]);
            return json_decode($response->getBody()->getContents(), true)['result'] ?? [];
        } catch (\Exception $e) {
            Log::error("Failed to get signatures for asset: " . $e->getMessage());
            return [];
        }
    }

    /**
     * 获取交易详情
     */
    public function getTransaction(string $signature)
    {
        try {
            $response = $this->client->post("{$this->baseUrl}/?api-key={$this->apiKey}", [
                'json' => [
                    'jsonrpc' => '2.0',
                    'id' => 1,
                    'method' => 'getTransaction',
                    'params' => [
                        $signature,
                        'json'
                    ]
                ]
            ]);
            return json_decode($response->getBody()->getContents(), true)['result'] ?? null;
        } catch (\Exception $e) {
            Log::error("Failed to get transaction: " . $e->getMessage());
            return null;
        }
    }
}
